<template>
  <div class="login-container">
    <c-form autoComplete="on" :model="loginForm" :rules="loginRules" ref="loginForm" label-position="left" label-width="0px"
      class="card-box login-form">
      <h3 class="title">用户登录</h3>
      <c-form-item prop="username">
        <span class="svg-container svg-container_login">
          <svg-icon icon-class="user" />
        </span>
        <c-input name="username" type="text" v-model="loginForm.username" @change="handleChange" autoComplete="on" placeholder="username" />
      </c-form-item>
      <c-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password"></svg-icon>
        </span>
        <c-input name="password" :type="pwdType" @keyup.enter.native="handleLogin" v-model="loginForm.password" autoComplete="on"
          placeholder="password"></c-input>
          <span class="show-pwd" @click="showPwd"><svg-icon icon-class="eye" /></span>
      </c-form-item>
      <c-form-item prop="randomCode">
        <c-row>
          <c-col :span="16">
            <c-input name="randomCode" type="text" @keyup.enter.native="handleLogin" v-model="loginForm.randomCode" autoComplete="on"
          placeholder="请输入图片验证码"></c-input>
          </c-col>
          <c-col :span="8" style="cursor: pointer">
            <img @load="handleCodeImgLoad" ref="codeImg" style="width: 100%" alt="" height="40" @click="handleGetRandomCode"/>
          </c-col>
        </c-row>
      </c-form-item>
      <c-form-item prop="tenantId">
        <span class="svg-container">
          <svg-icon icon-class="pc" />
        </span>
        <SelectConfig dataType="PROJECT" ref="project" :isLocal="isLocalProject" name="projectId" :getData="getData" v-model="loginForm.projectId" class="login-project"></SelectConfig>
      </c-form-item>

      <c-form-item>
        <c-button type="primary" style="width:100%;" :loading="loading" @click.native.prevent="handleLogin">
          登录
        </c-button>
      </c-form-item>
      <div class="tips" style="display:none;">
        <span style="margin-right:20px;">username: admin</span>
        <span> password: sa</span>
      </div>
    </c-form>
  </div>
</template>

<script>
import { isvalidUsername } from '@/utils/validate'
import SelectConfig from '@/components/Select/SelectConfig'
import request from '@/utils/request'
import axios from 'axios'

export default {
  name: 'login',
  data() {
    const validateUsername = (rule, value, callback) => {
      // if (!isvalidUsername(value)) {
      //   callback(new Error('请输入正确的用户名'))
      // } else {
      //   callback()
      // }
      if (value !== '') {
        callback()
      } else {
        callback(new Error('请输入正确的用户名'))
      }
    }
    const validatePass = (rule, value, callback) => {
      if (value.length < 2) {
        callback(new Error('密码不能小于2位'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: 'admin',
        password: '1qaz@WSX',
        randomCode: '',
        projectId: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePass }]
      },
      loading: false,
      isLocalProject: false,
      pwdType: 'password',
      randomCodeUrl: '/v1/randomCode/yqgenerate'
    }
  },
  computed: {
    token: this.$store.getters.token
  },
  mounted() {
    this.handleGetRandomCode()
  },
  methods: {
    handleCodeImgLoad() {
      window.URL.revokeObjectURL(this.codeImgUrl) // 清除释放
      this.codeImgUrl = null
    },
    handleGetRandomCode() {
      axios({
        url: this.randomCodeUrl + '?' + Math.random(),
        responseType: "blob",
        method: 'GET',
        headers: {
          "token": this.token,
          "Content-Type": "application/x-www-form-urlencoded"
        }
      }).then(res => {
        this.loginForm.randomCodeKey = res.headers.randomcodekey
        const objectUrl = window.URL.createObjectURL(res.data)
        this.$refs.codeImg.src = objectUrl
        this.codeImgUrl = objectUrl
      })
    },
    showPwd() {
      if (this.pwdType === 'password') {
        this.pwdType = ''
      } else {
        this.pwdType = 'password'
      }
    },
    handleChange(val) {
      this.loginForm.projectId = ''
      this.$refs.project.load()
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('Login', this.loginForm).then(() => {
            this.loading = false
            this.$router.push({ path: '/' })
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    getData() {
      return request({
        url: '/v1/yq/common/getProjectListByUserId',
        method: 'get',
        params: {
          userNo: this.loginForm.username
        }
      }).then(({ data }) => {
        return data.map(item => {
          return {
            value: /^\d+$/.test(item.project_id) ? parseInt(item.project_id) : item.project_id + '',
            label: item.project_name,
            ...item
          }
        })
      })
    }
  },
  components: {
    SelectConfig
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
  $bg:#b1b1b1;
  // $dark_gray:#889aa4;
  $dark_gray:#b1b1b1;
  $light_gray:#eee;

  .el-select{
    display: inline;
  }

  .login-container {
    position: fixed;
    height: 100%;
    width:100%;
    background-image: url("~@/assets/login-bg.png");
    background-repeat: no-repeat;
    background-size: cover;
    background-color: $bg;
    background-position: 0;
    input:-webkit-autofill {
      -webkit-box-shadow: 0 0 0px 1000px white inset !important;
      -moz-box-shadow: 0 0 0px 1000px white inset !important;
      box-shadow: 0 0 0px 1000px white inset !important;
      -webkit-text-fill-color: #424242 !important;
      -moz-text-fill-color: #424242 !important;
    }
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 40px;
      color: #424242;
    }
    .login-title{
      position:relative;
      top: 20%;
      left: 12%;
      color: white;
      font-size: 36px;
    }

    .el-input {
      display: inline-block;
      height: 40px;
      width: 85%;
    }
    .tips {
      font-size: 14px;
      color: #fff;
      margin-bottom: 10px;
    }
    .svg-container {
      padding: 6px 5px 6px 15px;
      color: $dark_gray;
      vertical-align: middle;
      width: 30px;
      display: inline-block;
      &_login {
        font-size: 20px;
      }
    }
    .title {
      font-size: 36px;
      color: white;
      margin: 0px auto 40px auto;
      text-align: center;
    }
    .login-form {
      position: relative;
      top: 29%;
      left: 10%;
      right: 0;
      width: 400px;
      padding: 35px 35px 15px 35px;
    }
    .el-form-item {
      background: white;
      border-radius: 5px;
      color: #424242;
      margin-bottom: 20px;
    }
    .el-form-item__content{
      line-height: 0px;
    }
    .login-project {
      width: 85%;
    }
    .show-pwd {
      position: absolute;
      right: 10px;
      top: 7px;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
      user-select:none;
    }
    .thirdparty-button{
      position: absolute;
      right: 35px;
      bottom: 28px;
    }
  }
</style>
